clear
*This file explores the attributes that predict being matched to an eviction case.
use unique_id landlord_ind  birthyear using "$build/experian_panel_clean.dta" if landlord_ind=="FALSE", clear /*No landlords in the sample*/ 
destring unique_id, replace
bys unique_id: keep if _n==1

*Bring in controls
merge m:1 unique_id using "$build/Data_ready_with_instruments.dta", keepusing(analysis_sample stringency_n100_loo filing_date tract case_year_cleaned evicted joint fem1 ad_damnum joint no_attorney filing_date district random_judge)
keep if _merge==3 | _merge==2
gen pinned=_merge==3
rename _merge _merge_controls

*Keep only cases that satisfy the case selection criteria
keep if analysis_sample==1

*Keep only cases at least 1 month after our earliest archive 
*(March 2005), since these are the only ones that we have the possibility of matching to  
gen courtdate_m = mofd(filing_date)
format %tm courtdate_m

keep if courtdate_m>(542+1) & courtdate_m<.

*Experian has 58.21 percent match rate
tab pinned

*Bring in neighborhood controls (from Collinson)
gen newtract=string(tract,"%06.0f")
replace newtract="17031"+newtract
drop tract
rename newtract tract
merge m:1 tract using "$build\census_vars_2010.dta", keepusing(sh_hhpov med_grossrent)
keep if _merge==1 | _merge==3
rename sh_hhpov neigh_pov_rate_5yr
rename med_grossrent nbrhd_med_rent_5yr
gen neigh_pov_rate_5yr_miss=(neigh_pov_rate_5yr==.)
gen nbrhd_med_rent_5yr_miss=(nbrhd_med_rent_5yr==.)
replace nbrhd_med_rent_5yr=-1 if nbrhd_med_rent_5yr==.
replace neigh_pov_rate_5yr=-1 if neigh_pov_rate_5yr==.
drop _merge

***********************
* Variable Processing *
***********************
*District-year and judge-year
gen filing_year=year(filing_date)
egen random_judge_id=group(random_judge)
egen judgeXyear=group(random_judge_id filing_year)

*District-quarter
gen quarter=qofd(filing_date)
format %tq quarter
egen districtXquarter=group(district quarter)

foreach var of varlist  ad_damnum nbrhd_med_rent_5yr {
	replace `var'= `var'/1000
}

foreach var of varlist joint fem1 ad_damnum  no_attorney stringency_n100_loo {
	gen `var'_miss=`var'==.
	replace `var'=-1 if `var'==.
}

macro define controls="fem1 joint no_attorney ad_damnum neigh_pov_rate_5yr nbrhd_med_rent_5yr neigh_pov_rate_5yr_miss nbrhd_med_rent_5yr_miss fem1_miss  "

**************
* REGRESSION *
**************
eststo pin1: areg pinned evicted $controls, a(districtXquarter) r cluster(judgeXyear) 
	local obs1: di %12.2gc e(N)
	estadd local obs "`obs1'", replace
	sum  pinned if e(sample)
	local ymean1: di %8.4fc r(mean)
	estadd local ymean "`ymean1'", replace
	
eststo pin2: areg pinned stringency_n100_loo stringency_n100_loo_miss $controls, a(districtXquarter) r cluster(judgeXyear) 
	local obs1: di %12.2gc e(N)
	estadd local obs "`obs1'", replace
	sum  pinned if e(sample)
	local ymean1: di %8.4fc r(mean)
	estadd local ymean "`ymean1'", replace
	
*******************
* EXPORT TO LATEX *
*******************
label var stringency_n100_loo "Judge stringency"
label var evicted "Evicted"
label var ad_damnum "Ad damnum (1000s)"
label var joint "Joint action"
label var neigh_pov_rate_5yr "Neighborhood poverty rate"
label var fem1 "Female (predicted)"
label var no_attorney "Tenant without attorney"
label var nbrhd_med_rent_5yr "Neighborhood median Rent"

esttab  pin1 pin2  using "${Inputs}/Cook_experian_matches", ///
replace compress wrap booktabs nonumbers nonotes label noconstant nomtitles se(3) b(3) order(evicted stringency_n100_loo fem1 ageatcase joint no_attorney ad_damnum nbrhd_med_rent_5yr neigh_pov_rate_5yr) ///
eqlabels(none) star(* 0.10 ** 0.05 *** 0.01) keep(evicted stringency_n100_loo fem1  joint no_attorney ad_damnum nbrhd_med_rent_5yr neigh_pov_rate_5yr )  ///
stats(obs r2 ymean, fmt(0 4 3) ///
labels( `"Number of observations"' `"\(R^{2}\)"' `"Mean of dep. var."') )
